﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Lastfm.Services;

namespace LastFmDemo
{
    public class Helper
    {
        public static string HttpGet(string url)
        {
            HttpWebRequest req = WebRequest.Create(url)
                                 as HttpWebRequest;
            string result = null;
            using (HttpWebResponse resp = req.GetResponse()
                                          as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
            return result;
        }
        public static void ViewBrowser(string url)
        {
            System.Diagnostics.Process.Start(url);
        }
        public static List<Track> SearchTrack(string title, Session session)
        {
            Lastfm.Services.TrackSearch trackSearch = new Lastfm.Services.TrackSearch(title, session);
            
            List<Lastfm.Services.Track> tracks = new List<Lastfm.Services.Track>();

            tracks = trackSearch.GetPage(1).ToList();

            return tracks;
        }
        public static List<Artist> SearchArtist(string title, Session session)
        {
            Lastfm.Services.ArtistSearch artistSearch = new Lastfm.Services.ArtistSearch(title, session);
            
            List<Artist> artists = new List<Lastfm.Services.Artist>();

            artists = artistSearch.GetPage(1).ToList();
            
            return artists;
        }

        public static List<Album> SearchAlbum(string title, Session session)
        {
            AlbumSearch albumSearch = new AlbumSearch(title, session);
            
            List<Album> albums = new List<Album>();

            albums = albumSearch.GetPage(1).ToList();

            return albums;
        }
        public static bool LogIn(string username, string password,Session session)
        {
            string md5password = Lastfm.Utilities.md5(password);
            try
            {
                session.Authenticate(username, md5password);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static List<Track> GetTopTrack(User user)
        {
            List<Lastfm.Services.TopTrack> toptracks = new List<Lastfm.Services.TopTrack>();
            toptracks = user.GetTopTracks().ToList();
            List<Lastfm.Services.Track> kq = new List<Lastfm.Services.Track>();
            for (int i = 0; i < toptracks.Count; i++)
            {

                kq.Add(toptracks[i].Item);
            }
            return kq;
        }
        public static List<Track> GetRecentTrack(User user)
        {
            List<Lastfm.Services.Track> recenttracks = new List<Lastfm.Services.Track>();
            recenttracks = user.GetRecentTracks().ToList();
            return recenttracks;
        }

        public static List<Track> GetLoveTrack(User user)
        {
            List<Lastfm.Services.Track> lovetracks = new List<Lastfm.Services.Track>();
            lovetracks = user.GetLovedTracks().ToList();
            return lovetracks;
        }
    }
}
